#!/usr/bin/python
# cookie use
# CookieJar  FileCookieJar MozillaCookieJar LWPCookieJar
# MozillaCookieJar (used by the Lynx and Netscape browsers)
# LWPCookieJar (format compatible with the libwww-perl library’s Set-Cookie3 file format. convenient to store cookies in a human-readable file)

import urllib2
import cookielib

# CookieJar

# cookie = cookielib.CookieJar()
# handler = urllib2.HTTPCookieProcessor(cookie)
# opener = urllib2.build_opener(handler)
# response = opener.open("http://www.baidu.com")

# for item in cookie:
# 	print("name:"+item.name)
# 	print("value:"+item.value)

# FileCookieJar not implement method save
# use MozillaCookieJar 

# save
file_name = "cookie.txt"
cookie = cookielib.MozillaCookieJar()
handler = urllib2.HTTPCookieProcessor(cookie)
opener = urllib2.build_opener(handler)
response = opener.open("http://www.baidu.com")
cookie.save(file_name,ignore_discard = True, ignore_expires = True)

# load
cookie = cookielib.MozillaCookieJar()
cookie.load(file_name, True, True)
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
req = urllib2.Request("http://www.baidu.com")
response = opener.open(req)
print(response.read())

